<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改用户</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f2f2f2;
            margin: 0;
            padding: 0;
        }
        .container {
            width: 50%;
            margin: 20px auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            color: #333;
        }
        form {
            display: flex;
            flex-direction: column;
        }
        label {
            margin-top: 10px;
        }
        input, select, button {
            margin-top: 5px;
            padding: 10px;
            border-radius: 5px;
            border: 1px solid #ccc;
        }
        button {
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        button:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<div class="container">
    <h1>修改用户</h1>
    <form id="updateUserForm">
        <label for="account">用户名:</label>
        <input type="text" name="account" id="account" required><br>

        <label for="name">姓名:</label>
        <input type="text" name="name" id="name" required><br>
        <label for="phone">电话:</label>
        <input type="tel" name="phone" id="phone" required><br>

        <label for="email">邮箱:</label>
        <input type="email" name="email" id="email" required><br>


        <label for="sex">性别:</label>
        <select name="sex" id="sex" required>
            <option value="1">男</option>
            <option value="0">女</option>
        </select><br>

        <label for="age">年龄:</label>
        <input type="number" name="age" id="age" min="0" max="120" required><br>

        <input type="submit" value="修改">
    </form>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
    $(document).ready(function() {
        const urlParams = new URLSearchParams(window.location.search);
        const userId = urlParams.get('userId');

        if (!userId) {
            alert('用户ID未提供');
            return;
        }

        $.ajax({
            url: "/users/" + userId,
            method: "GET",
            success: function(data) {
                $('#account').val(data.account || '');
                $('#name').val(data.name || '');
                $('#phone').val(data.phone || '');
                $('#email').val(data.email || '');
                $('#sex').val(data.sex || '1');
                $('#age').val(data.age || '');

            },
            error: function(error) {
                console.log("Error fetching user data", error);
            }
        });

        $('#updateUserForm').on('submit', function(e) {
            e.preventDefault();

            const userData = {
                account: $('#account').val(),
                name: $('#name').val(),
                phone: $('#phone').val(),
                email: $('#email').val(),
                sex: $('#sex').val(),
                age: $('#age').val(),


            };

            $.ajax({
                url: "/users/" + userId,
                method: "PATCH",
                contentType: "application/json",
                data: JSON.stringify(userData),
                success: function(data) {
                    alert('用户修改成功！');
                },
                error: function(error) {
                    alert('修改用户失败: ' + error.responseText);
                }
            });
        });
    });

</script>
</body>
</html>
